﻿// -----------------------------------------------------------------------
// <copyright file="UserInterface.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Exc2
{   
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class UserInterface
    {
        private GameBoard m_gameBoard = new GameBoard();
        private byte m_numberOfPlayers;

        public void StartGame()
        {
            Player playerO = new Player('O');
            bool endOfMatch = false;
            while (true)
            {
                try
                {
                    playerO.Name = UIInputOutput.GetPlayerName();
                }
                catch (TooLongException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }

                break;
            }

            while (true)
            {
                try
                {
                    m_gameBoard.CreateNewBoard(UIInputOutput.GetBoardSize());
                }
                catch (IllegalBoardSize ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }

                break;
            }

            m_numberOfPlayers = UIInputOutput.GetNumberOfPlayers();
            Player playerX = new Player('X');
            if (m_numberOfPlayers == 2)
            {
                playerX.Name = UIInputOutput.GetPlayerName();
            }
            else
            {
                playerX.Name = "Computer";
            }

            while (!endOfMatch)
            {
                playerO.InitializePlayerPawnsArray(m_gameBoard.Size);
                playerX.InitializePlayerPawnsArray(m_gameBoard.Size);
                m_gameBoard.initializeGameBoardWithPlayersPawns(playerO, playerX);
                UIBoardDisplay.PrintGameBoard(m_gameBoard);
                runGamePlay(playerO, playerX, m_gameBoard, m_numberOfPlayers);
                endOfMatch = UIInputOutput.CheckIfEndOfGame();
            }

            UIInputOutput.presentFinalScore(playerO, playerX);
        }

        public void runGamePlay(Player i_playerO, Player i_playerX, GameBoard i_gameBoard, int i_numOfPlayers)
        {
            char winningSymbol = ' ';
            bool notEndOfMatch = true;
            bool quitting = false;
            bool displayPreviousMove = false;
            string previousPlayerMove = null;
            CheckersMove moveFromUser = new CheckersMove();
            
            while (notEndOfMatch && !quitting)
            {
                bool hasTurn = true;
                CheckersGame.CheckForAvailableMoves(ref i_playerO, i_gameBoard);
                while (hasTurn && !quitting)
                {
                    if (displayPreviousMove)
                    {
                        System.Console.WriteLine(i_playerX.Name + "'s move was (" + i_playerX.Symbol + "): " + previousPlayerMove);
                    }

                    moveFromUser = UIInputOutput.GetMoveFromPlayer(i_playerO, ref quitting, ref previousPlayerMove);
                    if(quitting)
                    {
                        break;
                    }

                    hasTurn = CheckersGame.HandlePlayerTurn(i_playerO, i_playerX, i_gameBoard, moveFromUser);
                    UIBoardDisplay.PrintGameBoard(i_gameBoard);
                }

                hasTurn = true;
                displayPreviousMove = true;
                CheckersGame.CheckForAvailableMoves(ref i_playerX, i_gameBoard);
                while (hasTurn && !quitting)
                {
                    if (i_numOfPlayers == 2)
                    {
                        System.Console.WriteLine(i_playerO.Name + "'s move was (" + i_playerO.Symbol + "): " + previousPlayerMove);
                        moveFromUser = UIInputOutput.GetMoveFromPlayer(i_playerX, ref quitting, ref previousPlayerMove);
                        hasTurn = CheckersGame.HandlePlayerTurn(i_playerX, i_playerO, i_gameBoard, moveFromUser);
                    }
                    else
                    {
                        hasTurn = CheckersGame.HandlePlayerTurn(i_playerX, i_playerO, i_gameBoard, i_playerX.GeneratePlayerMove());
                    }

                    UIBoardDisplay.PrintGameBoard(i_gameBoard);
                }

                if (CheckersGame.IsEndOfMatch(i_playerO, i_playerX, ref winningSymbol))
                {
                    if (winningSymbol != 'T')
                    {
                        CheckersGame.UpdateMatchResult(i_playerO, i_playerX, winningSymbol);
                    }

                    notEndOfMatch = false;
                }
            }
        }
    }
}
